<template>
  <el-dialog
    :append-to-body="true"
    :close-on-click-modal="false"
    :before-close="cancel"
    :visible.sync="dialog"
    :title="isAdd ? '新增' : '编辑'"
    width="700px"
  >
    <el-form ref="form" :model="form" :rules="rules" :inline="true" label-width="80px">
      <el-form-item label="课程性质">
        <el-select @change="selectSystem" v-model="form.systemId" placeholder="请选择">
          <el-option
            v-for="item in courseSystem"
            :key="item.id"
            :label="item.name"
            :value="item.id"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="课程">
        <el-select v-model="form.cid" placeholder="请选择">
          <el-option
            v-for="item in course"
            :key="item.id"
            :label="item.name"
            :value="item.id"
          />
        </el-select>
      </el-form-item>

      <el-form-item label="课时">
        <el-select v-model="form.sectionId" placeholder="请选择">
          <el-option
            v-for="item in section"
            :key="item.id"
            :label="item.week + item.section"
            :value="item.id"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="周数">
        <el-select v-model="form.weekId" placeholder="请选择">
          <el-option
            v-for="item in week"
            :key="item.id"
            :label="item.time"
            :value="item.id"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="上课教师">
        <el-select v-model="form.teacherId" placeholder="请选择">
          <el-option
            v-for="item in teacher"
            :key="item.id"
            :label="item.username"
            :value="item.id"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="开课学期">
        <el-select v-model="form.teamId" placeholder="请选择">
          <el-option
            v-for="item in team"
            :key="item.id"
            :label="item.name"
            :value="item.id"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="上课教室" required>
        <el-input v-model="form.classroom"/>
      </el-form-item>
    </el-form>
    <div slot="footer" class="dialog-footer">
      <el-button type="text" @click="cancel">取消</el-button>
      <el-button :loading="loading" type="primary" @click="doSubmit">确认</el-button>
    </div>
  </el-dialog>
</template>

<script>
  import '@/api/admin/course/course'
  import {listajaxCourse} from '@/api/admin/plan/plan'
  import {
    add,
    listajaxSpecialty,
    listajaxTeam,
    listajaxCollege,
    listajaxCStatus,
    listajaxNature,
    listajaxSection,
    listajaxWay,
    listajaxWeek,
    listajaxTeacher,
    teacherCourseAdd
  } from "../../../api/admin/course/course";
  import {listajaxCourseSystem} from "../../../api/common/common";

  export default {
    props: {
      isAdd: {
        type: Boolean,
        required: true
      }
    },
    created() {
      listajaxWay().then(res => {
        this.way = res.records
      })
      listajaxCourseSystem().then(res => {
        this.courseSystem = res
      })
      listajaxTeam().then(res => {
        this.team = res.records
      })
      listajaxCStatus().then(res => {
        this.cstatus = res.records
      })
      listajaxCollege().then(res => {
        this.college = res
      })
      listajaxSection().then(res => {
        this.section = res.records
      })
      listajaxWeek().then(res => {
        this.week = res.records
      })
      listajaxTeacher().then(res => {
        this.teacher = res
      })
      if (this.$store.state.user.user.collegeId != null) {
        this.form['collegeId'] = this.$store.state.user.user.collegeId
      }
    },
    data() {
      return {
        loading: false, dialog: false,
        form: {
          cid:null,
          classroom: null,
          teamId: null,
          systemId: null,
          sectionId: null,
          weekId: null,
          teacherId: null,
          classesId:null,
          isClasses:1
        },
        course:[],
        rules: {},
        way: null,
        nature: null,
        team: null,
        cstatus: null,
        college: null,
        section: null,
        week: null,
        teacher: null
      }
    },
    methods: {
      load() {

      },
      loadCourse() {
        listajaxCourse(this.form).then(res => {
          this.course = res
        })
      },
      selectSystem(val){
        this.loadCourse()
      },
      cancel() {
        this.resetForm()
      },
      doSubmit() {
        this.loading = true
        if (this.isAdd) {
          this.doAdd()
        } else {
          this.doEdit()
        }
      },
      doAdd() {
        teacherCourseAdd(this.form).then(res => {
          this.resetForm()
          this.$notify({
            title: '添加成功',
            type: 'success',
            duration: 2500
          })
          this.loading = false
          this.$emit('close')
        }).catch(err => {
          this.loading = false
          console.log(err.response.data.message)
        })
      },
      doEdit() {
        edit(this.form).then(res => {
          this.resetForm()
          this.$notify({
            title: '修改成功',
            type: 'success',
            duration: 2500
          })
          this.loading = false
          // this.$parent.load()
          this.$emit('close')
        }).catch(err => {
          this.loading = false
          console.log(err.response.data.message)
        })
      },
      resetForm() {
        this.dialog = false
        this.$refs['form'].resetFields()
        this.form = {
          cid:null,
          classroom: null,
          teamId: null,
          systemId: null,
          sectionId: null,
          weekId: null,
          teacherId: null,
          classesId:null,
          isClasses:1
        }
      }
    }
  }
</script>

<style scoped>

</style>
